#!groovy

@Library('jenkinslib@master') _

def tools    = new org.devops.tools()
def dingmes  = new org.devops.ding()
def AppName  = "${env.JOB_NAME}"
def ImageTag = "11111"
def mvn_com  = "/usr/bin/mvn"

def remote = [:]
remote.name = 'dev'
remote.host = '172.16.1.108'
remote.user = 'root'
remote.password = '12345678'
remote.allowAnyHosts = true




pipeline {
    agent any

    stages {
        //代码拉取
        stage("CheckOut"){
            steps{
                script{            
                    tools.PrintMes("拉取代码...","green")       
                    checkout([$class: 'GitSCM', branches: [[name: '*/dev']], 
                                    extensions: [], 
                                    userRemoteConfigs: [[credentialsId: '87886427-ebc2-4f45-8921-dcf38f80867e', 
                                    url: 'http://gitlab.ebcall.com/xian-r-d-center/kg_product_20210101/esign-cloud.git']]])                
                }
            }
        }
        //代码静态检查 
        // stage("SonarQubeTesting"){
        //   steps{
        //     script{ 
        //     try {
        //          withSonarQubeEnv('SonarQube') {               
        //                 sh "cd ${env.JOB_NAME} && ${mvn_com} sonar:sonar -Dsonar.projectKey=test -Dsonar.java.binaries=target/sonar  -Dsonar.host.url=http://172.16.1.108:9000 -Dsonar.login=ccd849db55aa268b1914e9f67c5f29d01bc5ff26"
        //      }
        //     sleep(10)   //这里遇到一个坑  返回状态出现阻塞现象 加sleep 后正常
        //     timeout(15) {
        //             //利用sonar webhook功能通知pipeline代码检测结果，未通过质量阈，pipeline将会fail
        //             def qg = waitForQualityGate()
        //             echo "${qg}"
        //                 if (qg.status != 'OK') {
        //                   echo "${qg.status}"
        //                   error "未通过Sonarqube的代码质量阈检查，请及时修改！failure: ${qg.status}"
                          
        //                 }
        //              }
        //     } catch (exc) {
        //                  echo "111"
        //       } 
        //     echo "2222"
        //     }
        //   }
        // }  
        stage('SonarQube analysis') {
            steps {
                script {
                  withSonarQubeEnv('SonarQube') {
                    // Optionally use a Maven environment you've configured already                   
                     sh "cd ${env.JOB_NAME} && ${mvn_com} sonar:sonar -Dsonar.projectKey=test -Dsonar.java.binaries=target/sonar  -Dsonar.host.url=http://172.16.1.108:9000 -Dsonar.login=3233c85eccc057613337dcb118930013809cfcf3"
                  }
                }
            }
        }
        stage("Quality Gate") {
            steps {
                script {
                    sleep(15)
                    timeout(time: 1, unit: 'HOURS') {
                    // Parameter indicates whether to set pipeline to UNSTABLE if Quality Gate fails
                    // true = set pipeline to UNSTABLE, false = don't
                     //   waitForQualityGate abortPipeline: true
                        def qg = waitForQualityGate()
                        if (qg.status != 'OK') {
                            dingmes.HttpReq(AppName,ImageTag,"代码检查没有通过${qg.status} ❌")
                            failure
                        }
                        dingmes.HttpReq(AppName,ImageTag,"代码检查通过😄  😄  ")

                    }
                }
            }
        }

        //代码构建
        stage("Maven Build"){
            steps{
                script{
                    tools.PrintMes("mvn编译...","green")
                    sh "cd ${env.JOB_NAME}"
                    sh "pwd"
                }
                
            }

        }
        //ssh部署
        stage("deploy"){
            steps{
                script{
                    tools.PrintMes("部署到dev环境...","green")
                    writeFile file: 'abc.sh', text: 'ls -l'
                    sshCommand remote: remote, command: """if [ ! -d "/java/${env.JOB_NAME}" ]; then mkdir -p "/java/${env.JOB_NAME}";fi"""
                    sshPut remote: remote, from: 'abc.sh', into: "/java/${env.JOB_NAME}"
                }
            }
        }
        


    }
        post{
            success{
                script{
                    tools.PrintMes("========pipeline executed successfully========",'green')
                    dingmes.HttpReq(AppName,ImageTag,"构建成功 ✅ ")
                }
            }
            failure{
                script{
                    tools.PrintMes("========pipeline execution failed========",'red')
                    dingmes.HttpReq(AppName,ImageTag,"构建失败 ❌",CatchInfo)
                }
            }
            unstable{
                script{
                    tools.PrintMes("========pipeline execution unstable========",'red')
                    dingmes.HttpReq(AppName,ImageTag,"构建失败 ❌","不稳定异常")
                }
            }
            aborted{
                script{
                    tools.PrintMes("========pipeline execution aborted========",'blue')
                    dingmes.HttpReq(AppName,ImageTag,"构建失败 ❌","暂停或中断")
                }
            }
        }    
}
